/* Packages */
package edu.gatech.team404.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.gatech.team404.Course;
import edu.gatech.team404.dal.CourseDAL;
import edu.gatech.team404.tool.StatusCode;

/**
 * Servlet implementation class CourseCD
 * @author Brendan Cecere
 * @version 1.0
 */
@WebServlet("/CourseCD")
public class CourseCD extends HttpServlet {

	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	protected void processRequest(HttpServletRequest request, 
									HttpServletResponse response)
									throws IOException, NumberFormatException {
		
		response.setContentType("text/html;charset=UTF-8");
		final HttpSession session = request.getSession(true);
        final PrintWriter out = response.getWriter();
        final String statusTag = "courseCreationStatus";
        final String courseName = request.getParameter("courseName");
        
        final CourseDAL dal = new CourseDAL();
        try {
            final String name = request.getParameter("name");
            final String description = request.getParameter("description");
            final String instructor = request.getParameter("instructorName");
            
            boolean isValid = true;
            
            // Validate data
            if (name.isEmpty() ||
         	   instructor.isEmpty()) {
         	   isValid = false;
         	   session.setAttribute(statusTag, StatusCode.COURSE_CREATE_INVALID_DATA);
            }
            
            if (isValid) {
	            final Course course = new Course(name, description, instructor);
	            final boolean theDAL = dal.createCourse(course);
	            final boolean thingy = dal.signUpForCourse(instructor, course);
	            if(theDAL && thingy){
	            	session.setAttribute(statusTag, StatusCode.COURSE_CREATE_SUCCESS);
	            }
	            else{
	            	session.setAttribute(statusTag, StatusCode.COURSE_CREATE_FAIL);
	            }
            }
        }
        finally {
        	session.setAttribute("feed", StatusCode.CREATE.toString());
        	if (!courseName.equalsIgnoreCase("null")) {
        		response.sendRedirect("course.jsp?course=" 
            			+ URLEncoder.encode(courseName, "UTF-8"));
        	}
        	else {
        		response.sendRedirect("course.jsp");
        	}
        	dal.close();
        	out.close();
        }
	}
	
	/** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    } 

	/** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    }


}
